﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using FlanProjectData.Helpers;
using FlanProjectData.Managers;

namespace FlanProject.ScreenMenu
{
    public class MenuEntry
    {

        /***
         * Note : The texture height & width of entry MUST be indentic!
         **/

        private static Logger logger = new Logger("MenuEntry", Log_Type.WARNING);

        #region Attributs

        //the message of the button
        public string message { get; set; }
        public string url { get; set; }
        private SpriteFont font;
        //Vector2 textSize;
        public Vector2 textSize { get; set; }

        //if button is selected
        public bool Select { get; set; }

        public bool IsTextOnly { get; set; }
        public bool IsImage { get; set; }

        //the position of the button
        private Vector2 position;
        private Vector2 size;

        //the button's picture (it's not the same if it is selected or not)
        private Texture2D backgroundTexture;
        private Texture2D backgroundTextureSelected;

        //the colore of the text
		public Color color { get; set; }

        private float scalePercent; // A banir !
        private float scale;
        private bool proportionConserv;
        private float policeScale = 1;

        // Taille du message
        public Vector2 StringSize { get; set; }

        #endregion

        #region Constructor

        public MenuEntry(String message)
        {
            this.IsImage = false;
            this.message = message;
        }

        public MenuEntry(String message, Vector2 position, Vector2 size, float? scale)
        {
            this.message = message;
            this.position = position;
            this.size = size;
            this.Select = false;
            this.IsTextOnly = false;
            this.IsImage = false;
            if (scale != null)
            {
                this.policeScale = (float)scale;
            }
        }

        public MenuEntry(String message, Vector2 position, bool isOnlyAText, Color color, float? scale)
        {
            this.message = message;
            this.position = position;
            this.Select = false;
            this.IsTextOnly = true;
            this.IsImage = false;
            this.color = color;
            if (scale != null)
            {
                this.policeScale = (float)scale;
            }
        }

        public MenuEntry(bool isImage, Vector2 position, String url, Vector2? newSize, float? scale, bool proportionConserv)
        {
            this.message = "";
            this.position = position;
            this.url = url;
            if (newSize is Vector2)
            {
                this.size = (Vector2)newSize;
            }
            else { this.size = new Vector2(0, 0); }
            this.Select = false;
            this.IsTextOnly = false;
            this.IsImage = true;
            if (scale is float)
            {
                this.scale = (float)scale;
            }
            else { this.scale = 0; }
            this.proportionConserv = proportionConserv;
        }
        #endregion

        #region loading

        public void LoadContent(ContentManager content)
        {
            try
            {
                if (!this.IsTextOnly)
                {
                    if (this.IsImage)
                    {
                        
                        this.backgroundTexture = content.Load<Texture2D>(this.url);

                    }
                    else
                    {
                        logger.Debug("Size x:" + this.size.X + " y:" + this.size.Y);

                        this.backgroundTexture = content.Load<Texture2D>("Sprites\\Menus\\buttonFont");
                        this.backgroundTextureSelected = content.Load<Texture2D>("Sprites\\Menus\\buttonFontSelect");

                    }
                    this.calcPercent();
                }

                this.font = content.Load<SpriteFont>("Sprites\\Fonts\\menufont");
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
            }
        }
        #endregion

        #region Drawing & Updating
        public void Update()
        {
            if (!this.IsImage)
            {
                try
                {
                    //recuperation de la taille du message (avec la police) dans un vecteur
                    this.textSize = font.MeasureString(message);
                }
                catch (Exception e)
                {
                    logger.Error(e.Message);
                    logger.Error(e.StackTrace);
                }
            }

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            
                
                // Focus is on this MenuEntry
                if (this.Select)
                {
                   
                    if (!this.IsTextOnly)
                    {
                        if (IsImage == true)
                        {
                            int depPx = 2;
                            spriteBatch.Draw(this.backgroundTexture, new Rectangle((int)(this.position.X - depPx), (int)(this.position.Y - depPx), (int)(this.backgroundTexture.Width * this.scale + 2 * depPx), (int)(this.backgroundTexture.Height * this.scale + 2 * depPx)), Color.Red);
                            //spriteBatch.Draw(this.backgroundTexture, this.position, null, Color.White, (float)0.0,
                             //   new Vector2(0, 0), this.scalePercent, SpriteEffects.None, (float)0);
                        }
                        else
                        {
                            
                            spriteBatch.Draw(this.backgroundTextureSelected, this.position, null, Color.White, (float)0.0,
                                new Vector2(0, 0), this.scalePercent, SpriteEffects.None, (float)0);
                            
                            spriteBatch.DrawString(
                                this.font,
                                this.message,
                                new Vector2(((this.backgroundTextureSelected.Width * this.scalePercent - textSize.X) / 2) + this.position.X, ((this.backgroundTextureSelected.Height * this.scalePercent - textSize.Y) / 2) + this.position.Y),
                                Color.White,
                                (float)0,
                                new Vector2(0, 0),
                                policeScale,
                                SpriteEffects.None,
                                (float)0);
                        }
                        /*
                        spriteBatch.DrawString(this.font, this.message,
                            new Vector2(((this.backgroundTextureSelected.Width * this.scalePercent - textSize.X) / 2) + this.position.X,
                                ((this.backgroundTextureSelected.Height * this.scalePercent - textSize.Y) / 2) + this.position.Y),
                            Color.White);*/
                    }
                    else
                    {
                        spriteBatch.DrawString(
                            this.font,
                            this.message,
                            this.position,
                            Color.White,
                            (float)0,
                            new Vector2(0, 0),
                            policeScale,
                            SpriteEffects.None,
                            (float)0);
                        //spriteBatch.DrawString(this.font, this.message, this.position, Color.White);
                    }

                    
                }
                // Focus is NOT on this MenuEntry
                else
                {
                    if (!this.IsTextOnly)
                    {
                        if (this.IsImage)
                        {
                            if (this.proportionConserv)
                            {
                                if (Select == true)
                                {
                                    
                                    int depPx = 2;
                                    spriteBatch.Draw(this.backgroundTexture, new Rectangle((int)(this.position.X - depPx), (int)(this.position.Y - depPx), (int)(this.backgroundTexture.Width * this.scale + 2 * depPx), (int)(this.backgroundTexture.Height * this.scale + 2 * depPx)), Color.Red);
                                }
                                
                                spriteBatch.Draw(
                                    this.backgroundTexture,
                                    this.position,
                                    null,
                                    Color.White,
                                    (float)0.0,
                                    new Vector2(0, 0),
                                    this.scale,
                                    SpriteEffects.None,
                                    (float)0);
                            }
                            else
                            {
                                if (Select == true)
                                {
                                    int depPx = 2;
                                    
                                    spriteBatch.Draw(this.backgroundTexture, new Rectangle((int)this.position.X - depPx, (int)this.position.Y - depPx, (int)(this.size.X * this.backgroundTexture.Bounds.Height + depPx * 2), (int)(this.size.Y * (float)this.backgroundTexture.Bounds.Height + depPx * 2)), Color.Red);
                                }
                                
                                spriteBatch.Draw(
                                    this.backgroundTexture,
                                    new Rectangle((int)this.position.X, (int)this.position.Y, (int)(this.size.X * this.backgroundTexture.Bounds.Width), (int)(this.size.Y * (float)this.backgroundTexture.Bounds.Height)),
                                    null,
                                    Color.White,
                                    (float)0.0,
                                    new Vector2(0, 0),
                                    SpriteEffects.None,
                                    (float)0
                                );
                            }

                            //spriteBatch.Draw(this.backgroundTexture, this.position, null, Color.White, (float)0.0, new Vector2(0, 0), this.scalePercent, SpriteEffects.None, (float)0);
                        }
                        else
                        {
                            
                            spriteBatch.Draw(this.backgroundTexture, this.position, null, Color.White, (float)0.0,
                                new Vector2(0, 0), this.scalePercent, SpriteEffects.None, (float)0);
                            
                            spriteBatch.DrawString(this.font, this.message,
                                new Vector2(((this.backgroundTexture.Width * this.scalePercent - textSize.X) / 2) + this.position.X,
                                    ((this.backgroundTexture.Height * this.scalePercent - textSize.Y) / 2) + this.position.Y),
                                Color.Black);
                        }
                    }
                    else
                    {
                        spriteBatch.DrawString(
                            this.font,
                            this.message,
                            this.position,
                            this.color,
                            (float)0,
                            new Vector2(0, 0),
                            policeScale,
                            SpriteEffects.None,
                            (float)0);
                        //spriteBatch.DrawString(this.font, this.message, this.position, this.color);
                    }


                }
            
        }
        #endregion

        #region Methods
        //public void isSelected()
        //{
        //    this.select = true;
        //}
        //public void isUnSelected()
        //{
        //    this.select = false;
        //}

        //public void selection()
        //{
        //    if (this.select)
        //        this.select = false;
        //    else
        //        this.select = true;
        //}

        private void calcPercent()
        {
            float a = this.size.Y / (float)this.backgroundTexture.Bounds.Height;
            float b = this.size.X / (float)this.backgroundTexture.Bounds.Width;
            this.scalePercent = Math.Max(a, b);
            logger.Debug("Coef scale for background : " + this.scalePercent);
        }

        public void getStringSize()
        {
            StringSize =  font.MeasureString(this.message);
        }

        #endregion

        #region Events

        public event EventHandler<EventArgs> Selected;

        protected internal virtual void OnSelectEntry()
        {
            if (Selected != null)
                Selected(this, EventArgs.Empty);
        }

        #endregion

    }
}